require 'fastlane/action'
require_relative '../helper/upload_to_bugly_helper'

module Fastlane
  module Actions
    class UploadToBuglyAction < Action

      def self.run(params)
        cmd = []

        cmd << ["java -jar ~/bin/buglyqq-upload-symbol.jar"]
        cmd << "-appid #{params[:appid]}" 
        cmd << "-appkey #{params[:appkey]}" 
        cmd << "-bundleid #{params[:bundleid]}" 
        cmd << "-version #{params[:version]}" 
        cmd << "-platform #{params[:platform]}" 
        cmd << "-inputSymbol #{params[:inputSymbol]}" 

        Action.sh(cmd.join(' '), error_callback: lambda { |result|
          call_error_callback(params, result)
        })

      end

      def self.call_error_callback(params, result)
        if params[:error_callback]
          Dir.chdir(FastlaneCore::FastlaneFolder.path) do
            params[:error_callback].call(result)
          end
        else
          UI.shell_error!(result)
        end
      end

      #####################################################
      # @!group Documentation
      #####################################################

      def self.description
        "通过 buglyqq-upload-symbol.jar 来上传 .dSYM 到 bugly"
      end

      def self.available_options
        [
          FastlaneCore::ConfigItem.new(key: :appid,
                                       env_name: "FL_UPLOAD_TO_BUGLY_APPID", # The name of the environment variable
                                       description: "bugly appid", # a short description of this parameter
                                       verify_block: proc do |value|
                                          UI.user_error!("No appid for UploadToBuglyAction given, pass using `appid: 'xxx'`") unless (value and not value.empty?)
                                       end),
          FastlaneCore::ConfigItem.new(key: :appkey,
                                       env_name: "FL_UPLOAD_TO_BUGLY_APPKEY", # The name of the environment variable
                                       description: "bugly appkey", # a short description of this parameter
                                       verify_block: proc do |value|
                                          UI.user_error!("No appkey for UploadToBuglyAction given, pass using `appkey: 'xxx'`") unless (value and not value.empty?)
                                       end),
          FastlaneCore::ConfigItem.new(key: :bundleid,
                                       env_name: "FL_UPLOAD_TO_BUGLY_BUNDLEID", # The name of the environment variable
                                       description: "app bundleid", # a short description of this parameter
                                       verify_block: proc do |value|
                                          UI.user_error!("No bundleid for UploadToBuglyAction given, pass using `bundleid: 'xxx'`") unless (value and not value.empty?)
                                       end),
          FastlaneCore::ConfigItem.new(key: :version,
                                       env_name: "FL_UPLOAD_TO_BUGLY_VERSION", # The name of the environment variable
                                       description: "app version", # a short description of this parameter
                                       verify_block: proc do |value|
                                          UI.user_error!("No version for UploadToBuglyAction given, pass using `version: 'xxx'`") unless (value and not value.empty?)
                                       end),
          FastlaneCore::ConfigItem.new(key: :platform,
                                       env_name: "FL_UPLOAD_TO_BUGLY_PLATFORM",
                                       description: "app platform",
                                       optional: true,
                                       is_string: true, # true: verifies the input is a string, false: every kind of value
                                       default_value: "IOS"), # the default value if the user didn't provide one
          FastlaneCore::ConfigItem.new(key: :inputSymbol,
                                      env_name: "FL_UPLOAD_TO_BUGLY_INPUTSYMBOL", # The name of the environment variable
                                      description: ".dsym path", # a short description of this parameter
                                      verify_block: proc do |value|
                                        UI.user_error!("No inputSymbol for UploadToBuglyAction given, pass using `inputSymbol: 'xxx'`") unless (value and not value.empty?)
                                      end)
        ]
      end

      def self.authors
        ["fisherman"]
      end

      def self.details
        "upload .dSYM to bugly with buglyqq-upload-symbol.jar，具体操作查看 README.md"
      end

      def self.is_supported?(platform)
        platform == :ios
      end

      def self.example_code
        [
          'upload_to_bugly(
            appid: "xxx",
            appkey: "xxx",
            bundleid: "xxx",
            version: "a.b.c",
            inputSymbol: "./xxx.dSYM"
          )
          '
        ]
      end
    end
  end
end

